package com.example.seckill.system.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.sql.Driver;

/**
 * 代码生成器
 *
 * @author hl
 */
public class CodeGenerator {

    public static void main(String[] args) {
        String packgeName = "com.example.seckill.auth";
        CodeGenerator generator = new CodeGenerator();
        generator.generateByTables(packgeName, "success_seckilled");

    }

    private void generateByTables(String packageName, String... tableNames) {
        // 代码生成器
        AutoGenerator generator = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setActiveRecord(false)
                .setAuthor("hl")
                .setOutputDir(System.getProperty("user.dir") + "\\src\\main\\java")
                .setFileOverride(true)
                .setMapperName("%sMapper")
                .setServiceName("%sService")
                .setXmlName("%sMapper")
                .setServiceImplName("%sServiceImpl")
                .setIdType(IdType.UUID)
                // 设置时间格式为Timestamp
                .setDateType(DateType.SQL_PACK)
                .setOpen(false);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC")
                .setDriverName(Driver.class.getName())
                .setUsername("root")
                .setPassword("root123");

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageName)
                .setController("controller")
                .setEntity("entity")
                .setMapper("mapper")
                .setXml("mapping")
                .setService("service")
                .setServiceImpl("service.impl");


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setCapitalMode(true)
                .setRestControllerStyle(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setEntityTableFieldAnnotationEnable(true)
                .setInclude(tableNames);

        // 模板配置
        TemplateConfig tc = new TemplateConfig();
        tc.setEntity("/templates/entity.java.vm");
        tc.setMapper("/templates/mapper.java.vm");
        tc.setXml("/templates/mapper.xml.vm");
        tc.setService("/templates/service.java.vm");
        tc.setServiceImpl("/templates/serviceImpl.java.vm");
        tc.setController("/templates/controller.java.vm");

        generator.setGlobalConfig(gc)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategy)
                .setPackageInfo(pc)
                .setTemplate(tc)
                .execute();
    }
}
